package com.codejob.portal.web.action.project;

import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.codejob.portal.entity.project.Address;
import com.codejob.portal.entity.project.BidProject;
import com.codejob.portal.entity.project.Projects;
import com.codejob.portal.services.impl.project.AddressManager;
import com.codejob.portal.services.impl.project.BidProjectManager;
import com.codejob.portal.services.impl.project.ProjectManager;
import com.codejob.portal.services.intf.BusinessExcption;
import com.codejob.portal.web.BaseModelAction;
import com.codejob.portal.web.utils.Struts2Utils;

@Namespace("/project")
public class BidAction extends BaseModelAction<BidProject> {

	private static final long serialVersionUID = -1857408523237256120L;

	private Long id;

	private Long projId; 


	private Boolean hasBid;

	private BidProject bidProject;

	private Projects project;

	private List<Address> addressList;

	private List<BidProject> bidList;

	@Autowired
	private AddressManager addManager;

	@Autowired
	private ProjectManager projManager;

	@Autowired
	private BidProjectManager bidProjManager;

	public String readyBid() {
		if (!isLogin()) {
			return "login";
		}
		try {
			addressList = addManager.getList();
			project = projManager.get(projId);
			if (project != null) {
				Long userId=getUserId();
				Long tenderId=project.getTender().getId();
				hasBid = bidProjManager.hasBid(projId, userId);
				if (hasBid == false
						&& tenderId.longValue() == userId.longValue()) {
					hasBid = true;
				}
				Struts2Utils.getRequest().setAttribute("project", project);
				bidList = bidProjManager.getBidListByProjectId(projId);
			}
		} catch (BusinessExcption e) {
			//e.printStackTrace();
			return "error";
		}
		return "doBid";
	}

	public String doBid() {
		if (!isLogin()) {
			return "login";
		}
		try {
			bidProjManager.bidProject(bidProject, getUserId());
		} catch (BusinessExcption e) {
			//e.printStackTrace();
			return "error";
		}

		return "doBidSucc";
	}
	public String lookBid(){
		if (!isLogin()) {
			return "login";
		}
		try {
			bidProject=bidProjManager.getBidProject(bidProject.getId());
			
			if (bidProject != null) {
				project = bidProject.getProject();
				Long userId=getUserId();
				Long tenderId=project.getTender().getId();
				if ( tenderId.longValue() != userId.longValue()) {
					setUrl();
					return "login";
				}
				
				Struts2Utils.getRequest().setAttribute("project", project);
			}
		} catch (Exception e) {
			//e.printStackTrace();
			return "error";
		}
		return "lookBid";
	}
	
	public String chooseBid(){
		return "choose";
	}

	@Override
	public BidProject getModel() {
		return bidProject;
	}

	@Override
	public void prepare() throws Exception {
		if (id != null) {
			bidProject = bidProjManager.getBidProject(id);
		}
		if (bidProject == null) {
			bidProject = new BidProject();
		}
	}

	public BidProject getBidProject() {
		return bidProject;
	}

	public void setBidProject(BidProject bidProject) {
		this.bidProject = bidProject;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public Long getProjId() {
		return projId;
	}

	public void setProjId(Long projId) {
		this.projId = projId;
	}
	


	public Projects getProject() {
		return project;
	}

	public void setProject(Projects project) {
		this.project = project;
	}


	public List<Address> getAddressList() {
		return addressList;
	}

	public void setAddressList(List<Address> addressList) {
		this.addressList = addressList;
	}

	public List<BidProject> getBidList() {
		return bidList;
	}

	public void setBidList(List<BidProject> bidList) {
		this.bidList = bidList;
	}

	public Boolean getHasBid() {
		return hasBid;
	}

	public void setHasBid(Boolean hasBid) {
		this.hasBid = hasBid;
	}

}
